<?php

namespace app\common\service\store;


use app\admin\model\store\GoodsComment;
use app\admin\model\store\GoodsCommentUser;
use app\admin\model\user\User;
use app\common\service\BaseService;

class CommentService extends BaseService
{
    /**
     * Author:Zhongyuan Lu
     * 商品评论
     * @param $data
     * @return array
     */
    public static function comment($data)
    {
        $data['username'] = User::field('username')->find($data['uid'])->username;
        $bool = GoodsComment::create($data);
        if($bool) {
            return self::success('评论成功');
        }
        return self::fail('评论失败');
    }

    /**
     * Author:Zhongyuan Lu
     * 商品评论点赞
     * @param $data
     * @return array
     */
    public static function commentLike($where)
    {
        $data = GoodsCommentUser::where($where)->find();
        if ($data) {
            $bool = GoodsCommentUser::destroy($data->id);
            if($bool) {
                return self::success('取消点赞');
            }
            return self::fail('取消点赞失败');
        }

        $bool = GoodsCommentUser::create($where);
        if($bool) {
            return self::success('点赞成功');
        }
        return self::fail('点赞失败');
    }

    /**
     * Author:Yingbo Sun
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     */
    public static function CommentInfo($page, $limit, $where)
    {
        $model = new GoodsComment();
        $count = $model->where($where)->count();
        $list = $model->where($where)
            ->with('blurb')
            ->page($page, $limit)
            ->order('create_time','desc')
            ->select();
        if ($list){
            $list = $list->toArray();
        }else{
            $list = [];
        }

        $data = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];

        return $data;

    }

    /**
     * Author:Yingbo Sun
     * 评论回复
     * @param $id
     * @param $post
     * @param $row
     * @return mixed
     */
    public static function CommentReply($id,$post,$row)
    {
        $model = new GoodsComment();

        try {
            $save = $row->save($post);
            $model->where('id',$id)->update(['is_reply'=> 1]);
        } catch (\Exception $e) {
            return $save = false;
        }
        return $save;

    }
}
















